package cn.lyjuan.archetypes.bms.web.controller;

import cn.lyjuan.archetypes.bms.web.util.ErrUtils;
import cn.lyjuan.base.util.StringUtils;
import cn.lyjuan.archetypes.bms.web.bean.vo.BaseResp;
import cn.lyjuan.archetypes.bms.common.entity.MgAction;
import cn.lyjuan.archetypes.bms.common.entity.MgManage;
import cn.lyjuan.archetypes.bms.common.entity.MgRole;
import cn.lyjuan.archetypes.bms.web.service.ActionService;
import cn.lyjuan.archetypes.bms.web.service.PrivilegeService;
import cn.lyjuan.archetypes.bms.web.bean.dto.PrivilegeActionBean;
import cn.lyjuan.archetypes.bms.web.bean.dto.PrivilegeRoleBean;
import cn.lyjuan.archetypes.bms.web.bean.vo.PagerResp;
import cn.lyjuan.archetypes.bms.web.controller.base.SecurityController;
import cn.lyjuan.archetypes.bms.web.service.ManageService;
import cn.lyjuan.archetypes.bms.web.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("privilege")
public class PrivilegeController extends SecurityController
{
    @Autowired
    private ManageService manageSer;

    @Autowired
    private RoleService roleSer;

    @Autowired
    private ActionService actionSer;

    @Autowired
    private PrivilegeService privilegeSer;



    /* ############### 角色绑定操作 ############### */


    @RequestMapping(value = "role", method = RequestMethod.GET)
    public ModelAndView toRoleAction(Integer roleid, Model model)
    {
        model.addAttribute("roleid", roleid);// 供freemarker调用
        // 当前用户
        MgManage manage = this.getRealUser().getRealUser();

        // 用户有权限的所有角色
        List<MgRole> list = roleSer.queryRolesForManage(manage, null);

        this.pkgData(model, list);

        return mv("/privilege/privilege_role");
    }

    @RequestMapping(value = "role", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp<PrivilegeActionBean> roleAction(
            Integer roleid,
            @RequestParam(defaultValue = "1") Integer pn,
            @RequestParam(required = false) Integer ps)
    {
        ps = this.getPs();
        Map<String, String> params = this.paramsToMap();
        // 当前用户
        MgManage current = this.getRealUser().getRealUser();
        // 被操作的角色
        MgRole role = roleSer.queryRole(roleid);
        if (null == role)
            ErrUtils.appThrow("角色不存在或非您权限范围内！");
        // 当前用户所有的权限中标识角色拥有的权限
        List<PrivilegeActionBean> actions = privilegeSer.queryActionsWithManage(current, role, params, pn, ps);

        return BaseResp.resp(actions);
    }

    /**
     * 提交绑定操作
     * @return
     */
    @RequestMapping(value = "role", method = RequestMethod.PUT)
    @ResponseBody
    public BaseResp roleActionBind(
            Integer roleid,
            @RequestParam(required = false, name = "addIds")Integer[] addIds,
            @RequestParam(required = false, name = "subIds")Integer[] subIds)
    {
        // 当前用户
        MgManage current = this.getRealUser().getRealUser();

        // 被操作角色
        MgRole role = roleSer.queryRole(roleid);

        // 关联权限
        if (!StringUtils.isNullArray(addIds))
            privilegeSer.bindAction(current, role, addIds);

        // 取消关联
        if (!StringUtils.isNullArray(subIds))
            privilegeSer.unbindAction(role, subIds);
        return BaseResp.resp();
    }



    /* ############### 用户绑定角色 ############### */

    @RequestMapping(value = "manage", method = RequestMethod.GET)
    public ModelAndView toUserRole(Integer manageid, Model model)
    {
        model.addAttribute("manageid", manageid);// 供freemarker调用
        // 当前用户
        MgManage current = this.getRealUser().getRealUser();

        // 查询当前用户创建的所有用户
        List<MgManage> manages = manageSer.queryManages(current);

        this.pkgData(model, manages);

        return mv("/privilege/privilege_manage");
    }


    @RequestMapping(value = "manage", method = RequestMethod.POST)
    @ResponseBody
    public PagerResp<MgAction> userRole(
            Integer manageid,
            @RequestParam(defaultValue = "1") Integer pn,
            @RequestParam(required = false) Integer ps)
    {
        Map<String, String> params = this.paramsToMap();// 参数
        // 获取所有操作
        ps = this.getPs();
        // 当前用户
        MgManage current = this.getRealUser().getRealUser();

        // 被操作用户
        MgManage user = manageSer.queryById(manageid);

        // 查询有权限的所有角色
        List<PrivilegeRoleBean> roles = privilegeSer.queryRolesWithManage(current, user, params, pn, ps);

        return BaseResp.resp(roles);
    }

    /**
     * 提交绑定角色
     * @return
     */
    @RequestMapping(value = "manage", method = RequestMethod.PUT)
    @ResponseBody
    public BaseResp userRoleBind(
            Integer manageid,
            @RequestParam(required = false, name = "addIds")Integer[] addIds,
            @RequestParam(required = false, name = "subIds")Integer[] subIds)
    {
        // 当前用户
        MgManage current = this.getRealUser().getRealUser();

        // 被操作用户
        MgManage user = manageSer.queryById(manageid);

        // 关联权限
        if (!StringUtils.isNullArray(addIds))
            privilegeSer.bindRole(current, user, addIds);

        if (!StringUtils.isNullArray(subIds))
            privilegeSer.unbindRole(user, subIds);
        return BaseResp.resp();
    }
}
